*********************************************************************
8/22/07: I converted this to a macro and added the tax variables.

THESE NOTES WERE IN THE PROGRAM, BUT FOR SOME REASONS, THIS SCREWS UP
THE MACRO, SO I'VE MOVED THEM TO THE TOP

primary families (fkind=1), related subfamilies (fkind=2)
NOTE that the variable that is named ftype after 1987 is called 
fkind in 1987 andbefore.

DEFINE SM'S AS:
	never married, divorced, separated, or widowed
	at least one own never-married kid under 18
	primary families or unrelated subfamilies 


**********************************************************************;


options obs=max mprint;

%macro clean8087(yr);

proc sort data=cpsdata.all&yr;
by hseq;
run;

data poverty;
merge cpsdata.all&yr(in=in1) noncash.hous&yr;
by hseq;
if in1;
id=pseq*100+ppos;
run;

proc sort data=poverty;
by famid;
run;

data temp;
set noncash.fam&yr;
famid=pseq*100+ffposold;
run;
proc means;
run;

proc sort data=temp;
by famid;
run;

data poverty;
merge poverty(in=in1) temp;
by famid;
if in1;
run;

data temp;
set noncash.pers&yr;
ppos=ppposold;
run;
proc means;
run;
proc sort data=temp;
by pseq ppos;
run;
proc sort data=poverty;
by pseq ppos;
run;
data poverty;
merge poverty(in=in1) temp;
by pseq ppos;
if in1;

	  tottax=fedtax+statetax+fica-eitc;
	  tot_trans=ssinc+uiinc+pawval+ssi;
      lft5=floor(famid/100);


   	IF mcaid=1 THEN mcaid_yes=1;
  		 ELSE mcaid_yes=0;

	IF 0 LE age LE 4 THEN kid_0_4=1;
		 ELSE kid_0_4=0;

	IF age=0 THEN infant=1;
   		 ELSE infant=0;


/* BEFORE 1988, FAMREL ONLY DEFINED FOR RELATED AND UNRELATED SUBFAMILIES, 
  refrel2 IN (1,3) CAPTURES HEADS AND SPOUSES OF PRIMARY FAMILY WHILE
  famrel IN (1,2) CAPTURES HEADS AND SPOUSES OF SUBFMILIES */
	IF sex=2 AND (refrel2 IN (1,3) OR famrel IN (1,2)) THEN pot_wic_mom=1;
	   ELSE pot_wic_mom=0;

   run;


PROC SORT DATA=poverty;
   BY id;
RUN;
PROC SORT DATA=taxsim.taxs&yr;
   BY id;
RUN;
DATA poverty;
MERGE poverty(IN=in1) taxsim.taxs&yr(DROP=year);
BY id;
IF in1;

ts_tottax=ftl+stl+(ts_fica/2);

run;

PROC SORT DATA=poverty;
   BY famid;
RUN;
proc means sum noprint data=poverty;
   by famid;
   var ts_tottax;
   output out=tmp1(drop= _type_ _freq_) sum=ftax_nonprime;
run;

DATA poverty;
MERGE poverty(IN=in1) tmp1(KEEP=famid ftax_nonprime);
BY famid;
IF in1;
run;

PROC SORT DATA=poverty;
   BY hseq;
RUN;
proc means sum noprint data=poverty;
   by hseq;
   where fkind IN(1,2);
   var ts_tottax;
   output out=tmp2(drop= _type_ _freq_) sum=ftax_prime;
run;
proc means sum noprint data=poverty;
   by hseq;
   var ts_tottax;
   output out=tmp3(drop= _type_ _freq_) sum=hts_tottax;
run;

DATA poverty;
MERGE poverty(IN=in1) tmp2(KEEP=hseq ftax_prime) tmp3(KEEP=hseq hts_tottax);
BY hseq;
IF in1;

IF fkind IN(1,2) THEN fts_tottax=ftax_prime;
   ELSE fts_tottax=ftax_nonprime;

run;


proc sort data=poverty;
by famid;
run;

data temp1;
set poverty;
by famid;
if first.famid;
   IF marstat IN (5,6,7,8) AND fownu18 GT 0 AND fkind IN (1,3) 
      and sex=2 and 18 LE age LE 54 then sm_f=1;
   ELSE sm_f=0;


keep year sm_f ftype fkind hseq famid refrel2;
run;
DATA poverty;
MERGE poverty(IN=in1) temp1(keep=famid sm_f) ;
by famid;
run;

proc sort data=poverty;
by hseq;
run;

proc means max noprint data=poverty;
by hseq;
var refrel2 fkind;
   output out=temp3 max=fammax typemax;
run;

data temp2;
set temp1(rename=(sm_f=sm_h));
   IF fkind=1;
run;

proc sort data=temp2;
by hseq;
run;

DATA poverty;
MERGE poverty(IN=in1) temp2(keep=hseq sm_h) temp3(keep=hseq fammax typemax);
by hseq;
   IF fkind ne 2 then sm=sm_f;
   ELSE IF fkind=2 then sm=sm_h;

   IF fkind=1 and sm=1 and fammax LE 4 and typemax=1 then smlone=1;
   ELSE smlone=0;
   
run;


proc sort data=poverty;
by famid;
run;
proc means sum noprint data=poverty;
   by famid;
   var tottax fedtax statetax fica eitc pcapgain pcaploss emcontrb tot_trans ssinc mcaid_yes kid_0_4 wkswrk infant pot_wic_mom pawval;
   output out=temp sum=ftottax ffedtax fstatetax ffica feitc fcapgain fcaploss femcontrb ftot_trans fssinc_rr
					   fmcaid fkid_0_4 fwkswrk finfant fpot_wic_mom fpawval;
run;
data poverty;
   merge poverty 
         temp (keep=famid ftottax ffedtax fstatetax ffica feitc fcapgain fcaploss femcontrb ftot_trans fssinc_rr
				    fmcaid fkid_0_4 fwkswrk finfant fpot_wic_mom fpawval);
   by famid; 

   fwic_mom=MIN(fpot_wic_mom,finfant,1);

   /* BEFORE 1988, FAMREL ONLY DEFINED FOR RELATED AND UNRELATED SUBFAMILIES, 
  refrel2 IN (1,3) CAPTURES HEADS AND SPOUSES OF PRIMARY FAMILY WHILE
  famrel IN (1,2) CAPTURES HEADS AND SPOUSES OF SUBFMILIES */
	IF sex=2 AND finfant GT 0 AND (refrel2 IN (1,3) OR famrel IN (1,2)) THEN wic_mom=1;
      ELSE wic_mom=0;
 

run;

data temp;
   set poverty; 
   if fkind=1;
run;

data temp;
   set temp; 
   by famid notsorted;
   if first.famid=1 then dummy=1;
   else dummy=0;
   if dummy=1;
keep lft5 ftotval fmvfs fhoussub fmvrsl fngmcare fngmcaid ftotearn fpersons frelu18 fpovcut;
run;

proc sort data=poverty;
by lft5;
run;
proc sort data=temp;
by lft5;
run;

data poverty;
merge poverty temp(rename=(ftotval=ftotval_new fmvfs=fmvfs_new
     fhoussub=fhoussub_new fmvrsl=fmvrsl_new 
     fngmcare=fngmcare_new fngmcaid=fngmcaid_new ftotearn=ftotearn_new fpersons=fpersons_new 
     frelu18=frelu18_new fpovcut=fpovcut_new));
by lft5;

if fkind=2 then do;
  	 ftotval_final=ftotval_new;
     fmvfs_final=fmvfs_new;
         fhoussub_final=fhoussub_new; 
         fmvrsl_final=fmvrsl_new;
         fngmcare_final=fngmcare_new; 
         fngmcaid_final=fngmcaid_new;
         ftotearn_final=ftotearn_new; 
		 fpersons_final=fpersons_new;
	     frelu18_final=frelu18_new; 
	     fpovcut_final=fpovcut_new; 
end;
else if fkind ne 2 then do;
        ftotval_final=ftotval;
        fmvfs_final=fmvfs;
         fhoussub_final=fhoussub; 
         fmvrsl_final=fmvrsl;
         fngmcare_final=fngmcare; 
         fngmcaid_final=fngmcaid; 
		 ftotearn_final=ftotearn; 
		 fpersons_final=fpersons;
	     frelu18_final=frelu18; 
	     fpovcut_final=fpovcut; 
end;

run;

proc means sum noprint data=poverty;
by lft5;
where fkind in(1,2);
var tottax eitc pcapgain pcaploss emcontrb tot_trans ssinc mcaid_yes kid_0_4 wkswrk pawval wic_mom infant;
output out=temp sum=ftottax2 feitc2 fcapgain2 fcaploss2 femcontrb2 ftot_trans2 fssinc_rr2
					fmcaid2 fkid_0_42 fwkswrk2 fpawval2 fwic_mom2 finfant2;
run;

data poverty;
merge poverty temp (keep=lft5 ftottax2 feitc2 fcapgain2 fcaploss2 femcontrb2 ftot_trans2 fssinc_rr2
						 fmcaid2 fkid_0_42 fwkswrk2 fpawval2 fwic_mom2 finfant2);
by lft5; 

if fkind in (1,2) then do;
   ftottax_final=ftottax2;
   feitc_final=feitc2;
	fcapgain_final=fcapgain2;
     fcaploss_final=fcaploss2;
	 femcontrb_final=femcontrb2;
	 ftot_trans_final=ftot_trans2;
	 fssinc_rr_final=fssinc_rr2;

	 fmcaid_final= fmcaid2;
	 fkid_0_4_final= fkid_0_42;
	 fwkswrk_final=fwkswrk2;
	 fpawval_final=fpawval2;
	 fwic_mom_final=fwic_mom2;
	 finfant_final=finfant2;

end;
else if fkind in (3,4,5) then do;
   ftottax_final=ftottax;
   feitc_final=feitc;
	fcapgain_final=fcapgain;
     fcaploss_final=fcaploss;
	 femcontrb_final=femcontrb;
	 ftot_trans_final=ftot_trans;
	 fssinc_rr_final=fssinc_rr;

	 fmcaid_final= fmcaid;
	 fkid_0_4_final= fkid_0_4;
	 fwkswrk_final=fwkswrk;
	 fpawval_final=fpawval;
	 fwic_mom_final=fwic_mom;
	 finfant_final=finfant;
end;

run;
proc sort data=poverty;
by famid;
run;
data temp;
   set poverty;
   by famid;
   if first.famid; 
run;
proc sort data=temp;
   by hseq;
run;


proc means sum noprint data=temp;
   by hseq;
   where fkind NE 2;
   var fmvfs fhoussub fmvrsl fngmcare fngmcaid ftotearn fpersons frelu18;
   output out=temp1 sum=hmvfs hhoussub hmvrsl hngmcare hngmcaid htotearn hpersons hrelu18;
run;

proc sort data=poverty;
   by hseq;
run;
proc means sum noprint data=poverty;
   by hseq;
   var tottax fedtax statetax fica eitc pcapgain pcaploss emcontrb tot_trans ssinc;
   output out=temp sum=htottax hfedtax hstatetax hfica heitc hcapgain hcaploss hemcontrb htot_trans hssinc_rr;
run;

data cpsdata.poverty&yr;
   merge poverty 
         temp (keep= hseq htottax hfedtax hstatetax hfica heitc hcapgain hcaploss hemcontrb htot_trans hssinc_rr)
		 temp1 (keep=hseq hmvfs hhoussub hmvrsl hngmcare hngmcaid htotearn hpersons hrelu18);
   by hseq; 
run;
proc means;
run;

%mend;

%clean8087(1980);
%clean8087(1981);
%clean8087(1982);
%clean8087(1983);
%clean8087(1984);
%clean8087(1985);
%clean8087(1986);
%clean8087(1987);


/*
proc compare base=cpsdata.poverty1980_bak
             compare=cpsdata.poverty1980
			 novalues listall
;

run;
*/
